package tau_sadna.tau_map;

import android.graphics.Color;
import android.text.Html;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class HelpWindow extends LinearLayout {

	private MainActivity mainActivity;
	private Button closeButton;
	private ScrollView scrollView;

	public HelpWindow(MainActivity main) {
		super(main);
		mainActivity = main;
		MLString helpText = new MLString(getResources().getString(R.string.help_en),
								getResources().getString(R.string.help_he));
		MLString closeText = new MLString(getResources().getString(R.string.close_en),
							getResources().getString(R.string.close_he));
		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
		layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
		layoutParams.addRule(RelativeLayout.BELOW, R.id.search_box);
		setLayoutParams(layoutParams);
		setBackgroundColor(Color.WHITE);
		
		scrollView = new ScrollView(main);
		
		TextView textView = new TextView(main);
		if (MainActivity.language == MainActivity.ENGLISH)
			textView.setGravity(Gravity.LEFT);
		else
			textView.setGravity(Gravity.RIGHT);
		textView.setTextSize(20);
		textView.setText(Html.fromHtml(helpText.getName(),new ResourceImageGetter(main),null));
		
		scrollView.addView(textView);

		closeButton = new Button(main);
		closeButton.setText(closeText.getName());
		closeButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				mainActivity.closeCurrentWindow();
			}
		});
		
		LinearLayout.LayoutParams closeParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		
		setOrientation(VERTICAL);
		setGravity(Gravity.CENTER_HORIZONTAL);
		addView(scrollView);
		addView(closeButton,closeParams);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		closeButton.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
		Log.i("HelpWindow","onMeasure tot_mh="+getMeasuredHeight()+"mh="+closeButton.getMeasuredHeight());
		LayoutParams scrollParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				getMeasuredHeight()-closeButton.getMeasuredHeight(), 0);
		scrollView.setLayoutParams(scrollParams);
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
}
